package christ.mmd.xml;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.graphics.Bitmap;
import android.widget.ImageView;

import christ.mmd.bl.Community;
import christ.mmd.bl.FileUpdateInfo;
import christ.mmd.bl.Holiday;
import christ.mmd.bl.Image;

public class HolidaysXmlParser extends AXmlParser {

	
	private static String IMG_BASE_URL = "http://dl.dropbox.com/u/10342089/Catalog/MakeMyDay/img/holidays/";
	@Override
	protected List readDataEntities(XmlPullParser parser)
			throws XmlPullParserException, IOException {
		List entries = new ArrayList();

		parser.require(XmlPullParser.START_TAG, ns, "holidays");
		while (parser.next() != XmlPullParser.END_TAG) {
			if (parser.getEventType() != XmlPullParser.START_TAG) {
				continue;
			}
			String name = parser.getName();
			// Starts by looking for the entry tag
			if (name.equals("holiday")) {
				entries.add(readHolidayEntry(parser));
			} else {
				skip(parser);
			}
		}
		return entries;
	}

	// Processes file tags in the feed.
	private Holiday readHolidayEntry(XmlPullParser parser)
			throws IOException, XmlPullParserException {
		parser.require(XmlPullParser.START_TAG, ns, "holiday");

		String id = parser.getAttributeValue(null, "id");
		String dateStr = parser.getAttributeValue(null, "date");
		String title = parser.getAttributeValue(null, "title");
		String articleUrl = parser.getAttributeValue(null, "articleUrl");
		String description = "";
		parser.nextTag();
		String tagName = parser.getName();
		// Starts by looking for the entry tag
		if (tagName.equals("description")) {
			parser.require(XmlPullParser.START_TAG, ns, "description");
			description = readText(parser);
			parser.require(XmlPullParser.END_TAG, ns, "description");
			parser.nextTag();
		}
		tagName = parser.getName();
		ArrayList<Image> images = new ArrayList<Image>();
		if (tagName.equals("images")) {
			parser.require(XmlPullParser.START_TAG, ns, "images");
			parser.nextTag();
			tagName = parser.getName();
			while (tagName.equals("image")) {
				parser.require(XmlPullParser.START_TAG, ns, "image");
				String type = parser.getAttributeValue(null, "type");
				String url = IMG_BASE_URL+ parser.getAttributeValue(null, "url");
				images.add(new Image(type, url));
				parser.nextTag();
				parser.require(XmlPullParser.END_TAG, ns, "image");
				parser.nextTag();
				tagName = parser.getName();
			}
			parser.require(XmlPullParser.END_TAG, ns, "images");
			parser.nextTag();
		}
		parser.require(XmlPullParser.END_TAG, ns, "holiday");
		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy");
		Date date = null;
		try {
			date = format.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		long idLong = id.isEmpty() ? -1 : Long.parseLong(id);
		Holiday result = new Holiday(idLong, date, title, articleUrl, description, images);
		return result;
	}

}
